const User = require("../model/use.model")

class UserServer {
    async createUser(user_name, password) {
        // todo:写入数据库
        const res = await User.create({
            user_name, password
        })
        return res
    }

    async getUserInfo({id, user_name, password, is_admin}) {
        const whereOpt = {}
        id && Object.assign(whereOpt, {id})
        user_name && Object.assign(whereOpt, {user_name})
        password && Object.assign(whereOpt, {password})
        is_admin && Object.assign(whereOpt, {is_admin})
        const res = await User.findOne({
            attributes: ['id', 'user_name', "password", "is_admin"], where: whereOpt
        })
        return res ? res.dataValues : null
    }
    // 修改密码
    async updateById({id, user_name, password, is_admin}) {
        const whereOpt = {id}
        const newUser = {}

        user_name && Object.assign(newUser, {user_name})
        password && Object.assign(newUser, {password})
        is_admin && Object.assign(newUser, {is_admin})

        const res = await User.update(
            newUser, {
                where: whereOpt
            });
        console.log(res[0])
        return res[0] ? 1 : 0
    }
}

module.exports = new UserServer()